package com.yiren.dbaa.module.practice_set.site.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.site.contract.VersionHistoryContract;
import com.yiren.dbaa.module.practice_set.site.presenter.VersionHistoryPresenter;
import com.yiren.dbaa.module.practice_set.site.view.adapter.VersionAdapter;
import com.yiren.dbaa.view.dialog.LoadDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/30 15:03
 * @description: 历史版本记录
 */
public class VersionHistoryActivity extends BaseActivity implements VersionHistoryContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_version_rule)
    ListView lvVersionRule;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;

    private VersionHistoryContract.Presenter versionHistoryPresenter;
    private SiteEntity site;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version);
        ButterKnife.bind(this);
        tvTitle.setText("回退版本");
        site = (SiteEntity) getIntent().getSerializableExtra("site");
        init();
    }

    private void init() {
        versionHistoryPresenter = new VersionHistoryPresenter(this);
        UserParamConfigBean userParamConfigBean = LoadConfigApi.loadUserParamConfig(this);
        if (site != null) {
            versionHistoryPresenter.loadVersionList(this, userParamConfigBean.getUserInfo().getId(), site.getSiteCode());
        }
    }

    @OnClick(R.id.iv_back)
    public void onClick() {
        finish();
    }

    @Override
    public void showVersionList(List<SiteEntity> siteList) {
        if (siteList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            lvVersionRule.setVisibility(View.GONE);
            tvTips.setText("当前暂无历史版本数据!");
        } else {
            llNoData.setVisibility(View.GONE);
            lvVersionRule.setVisibility(View.VISIBLE);
            VersionAdapter versionAdapter = new VersionAdapter(this, siteList);
            lvVersionRule.setAdapter(versionAdapter);
            versionAdapter.setOnManageClickListener(position -> {
                CommonDialog.showTwoBtn(this, "操作确认", "下载会覆盖本地考规，确定下载吗？", "取消", "下载", (dialog, which) -> {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        siteList.get(position).setSiteName(site.getSiteName());
                        siteList.get(position).setRuleVerLast(site.getRuleVerLast());
                        versionHistoryPresenter.downLoadSiteRuleData(siteList.get(position), this);
                        dialog.dismiss();
                    }
                });
            });
        }
    }

    @Override
    public void downLoadSuccess() {
        LoadDialog.dismiss(this);
        runOnUiThread(() -> {
            CommonDialog.showTwoBtn(VersionHistoryActivity.this, "操作确认", "下载已完成，请问是直接发布，还是编辑规则？", "直接发布", "编辑规则", (dialog, which) -> {
                if (which == -2) {
                    finish();
                } else {
                    finish();
                }
                dialog.dismiss();
            });
        });
    }

}